<?php

	session_start();

	include("/var/www/html/vivencias/conexaoBD.php");

	include("/var/www/html/vivencias/verificaRecursosAtivados.php");

	$idTopico = $_GET['idTopico'];
	$grupo    = $_GET['grupo'];
	$mensagem = '';

	//Verifica o grupo do usuario
	$queryGrupo     = "SELECT grupo FROM usuariosCursos WHERE idUsuario='".$_SESSION['idUsuario']."' AND idCurso='".$_SESSION['idCurso']."'";
	$resultadoGrupo = mysql_query($queryGrupo,$conexao);
	$objetoGrupo	= mysql_fetch_object($resultadoGrupo);

	//O usuario so podera editar o texto se pertencer ao grupo que elaborou o mesmo ou se for um texto coletivo ou se for prof/tutor/admin
	if(($grupo == $objetoGrupo->grupo) || ($grupo == '0') || ($_SESSION['idTipoUsuario'] == '0') || ($_SESSION['idTipoUsuario'] == '1') || ($_SESSION['idTipoUsuario'] == '5')){

		//Verifica se ja existe alguem editando o texto naquele momento
		$queryVerifica     = "SELECT sendoEditado,data,hora,idUsuario FROM wikiTopicos WHERE idTopico='".$idTopico."'";
		$resultadoVerifica = mysql_query($queryVerifica,$conexao);
		$objetoVerifica    = mysql_fetch_object($resultadoVerifica);
		if($objetoVerifica->sendoEditado){
			$dataBanco = ereg_replace("([0-9]{2}([0-9]{2})?)[/.-]([01]?[0-9])[/.-]([0123]?[0-9])","\\4-\\3-\\1",$objetoVerifica->data);
			$dataAtual = date('d-m-Y');
			if($dataBanco == $dataAtual){
				$horaAtual = date('G:i:s');
				$horaAtualExplode = explode(':',$horaAtual);
				$horaBanco = explode(':',$objetoVerifica->hora);

				$diferencaHora = $horaAtualExplode[0] - $horaBanco[0];//Diminuindo as horas
				if(($diferencaHora < 3) && ($_SESSION['idUsuario'] != $objetoVerifica->idUsuario)){
					$query     = "SELECT nome FROM usuarios WHERE idUsuario=".$objetoVerifica->idUsuario.";";
					$resultado = mysql_query($query,$conexao);
					$objeto    = mysql_fetch_object($resultado);

					$disabled = 'disabled';
					$mensagem = 'Texto sendo editado por '.$objeto->nome.' neste momento.';
				}else{
					$disabled = '';
				}

			}else if($dataBanco < $dataAtual){ //o usuario saiu do site sem des-setar o sendoEditado
				$disabled = '';
			}
		}

	}else{
		$disabled = 'disabled';
	}

	if(!$disabled){
		//Existe uma pessoa editando o texto naquele momento
		$queryEdita     = "UPDATE wikiTopicos SET sendoEditado=1,data=NOW(),hora=NOW(),idUsuario=".$_SESSION['idUsuario']." WHERE idTopico='".$idTopico."'";
		$resultadoEdita = mysql_query($queryEdita,$conexao);
	}

	$query     = "SELECT wikiConteudo.*,wikiTopicos.topico FROM wikiConteudo,wikiTopicos WHERE wikiConteudo.idTopico='".$idTopico."' AND wikiConteudo.idTopico = wikiTopicos.idTopico";
	$resultado = mysql_query($query,$conexao);
	$objeto    = mysql_fetch_object($resultado);


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Experi&ecirc;ncias do Processo de Adoecimento</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="vivencias.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function mudaCor(x){
	document.getElementById(x).style.color="black";

}
</script>
<script type="text/javascript">
function deslogaUsuario(y){
	if(y==1){
		location.href='wikiDeslogaUsuario.php?idTopico=<?php echo $idTopico;?>';
	}
}

</script>
<script language="JavaScript" type="text/JavaScript">

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>
<body onLoad="MM_preloadImages('imagens/stt_salaR2.jpg','imagens/stt_gloss2.jpg','imagens/stt_blogs2.jpg')">
<div align="center">
  <table width="1000" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="10%" align="left" valign="top"><strong><img src="imagens/logo.jpg" width="66" height="53"></strong></td>
                  <td width="90%"><strong><img src="imagens/tit1.jpg" width="612" height="28"></strong></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr align="left" valign="middle">
                  <td colspan="3" class="nomecurso"><?php echo $_SESSION['nomeCurso'];?></td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td><strong><a href="index.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/sair.gif" width="33" height="19" border="0"></a></strong></td>
                </tr>
                <tr align="left" valign="middle">
                  <td width="25%">&nbsp;</td>
                  <td width="22%">&nbsp;</td>
                  <td width="52%">&nbsp;</td>
                  <td width="0%">&nbsp;</td>
                  <td width="0%">&nbsp;</td>
                  <td width="1%">&nbsp;</td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td><table width="96%" border="0" cellspacing="0" cellpadding="0">
                <tr align="left" valign="top">
		  <?php if(($_SESSION['idTipoUsuario']=="0")||($_SESSION['idTipoUsuario']=="1")||($_SESSION['idTipoUsuario'])=="5"){?>
                  <td width="3%" background="imagens/bar_fnd_8.jpg" bgcolor="EBF4F9"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="relatorios.htm?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_8.jpg" width="23" height="161" border="0"></a></strong></td>
		  <?php } ?>
                  <td width="3%" background="imagens/bar_fnd_7.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="info.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_7.jpg" width="23" height="137" border="0"></a></strong></td>
                  <td width="3%" background="imagens/bar_fnd_6.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="esppess.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_6.jpg" width="23" height="156" border="0"></a></strong></td>
                  <td width="3%" background="imagens/bar_fnd_5.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="comunicacao.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_5.jpg" width="23" height="146" border="0"></a></strong></td>
                  <td width="3%" background="imagens/bar_fnd_4.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="banco.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_4.jpg" width="23" height="219" border="0"></a></strong></td>
                  <td width="3%" background="imagens/bar_fnd_3.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="ferramentasTrabalho.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_3.jpg" width="23" height="249" border="0"></a></strong></td>
                  <td width="71%" align="center" bgcolor="F5FEFF">
<table width="816" border="0" cellpadding="0" cellspacing="0">
                      <tr align="left" valign="top">
		 <?php
			if($objetoN->salaReuniao){
				if($_SESSION['idTipoUsuario'] != '3'){
		 ?>
                        <td width="10" height="21"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                        <td width="129"><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="salaR.php?idTopico=<?php echo $idTopico;?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('salareuniao','','imagens/stt_salaR2.jpg',1)"><img src="imagens/stt_salaR1.jpg" name="salareuniao" width="129" height="21" border="0"></a></td>
		<?php }} ?>
                        <td width="10"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                        <td width="56"><a href="wiki.php"><img src="imagens/stt_wiki2.jpg" width="41" height="21" border="0"></a></td>
		<?php
			if(($objetoN->glossario && $glossario)||($objetoN->glossario && $_SESSION['idTipoUsuario']=="0")||($objetoN->glossario && $_SESSION['idTipoUsuario']=="1")||($objetoN->glossario && $_SESSION['idTipoUsuario']=="5")){
		?>

                        <td width="10"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                        <td width="91"><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="glossario.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('glossario','','imagens/stt_gloss2.jpg',1)"><img src="imagens/stt_gloss1.jpg" name="glossario" width="78" height="21" border="0"></a></td>
		<?php } ?>

		<?php
			if( ($objetoN->blog && $blog) || ($objetoN->blog && $_SESSION['idTipoUsuario']=="0") || ($objetoN->blog && $_SESSION['idTipoUsuario']=="1") || ($objetoN->blog && $_SESSION['idTipoUsuario']=="5") ){
		?>
                        <td width="10"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                        <td width="48"><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="blogs.php?idTopico=<?php echo $idTopico;?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('blogs1','','imagens/stt_blogs2.jpg',1)"><img src="imagens/stt_blogs1.jpg" name="blogs1" width="48" height="21" border="0" id="blogs1"></a></td>
		<?php } ?>
                        <td width="234"><img src="imagens/pix_branco.gif" width="230" height="5"></td>
                      </tr>
                      <tr align="center" valign="middle">
                           <td height="2" colspan="9"><img src="imagens/pix_cor2d.gif" width="810" height="2"></td>
                      </tr>
                      <tr align="center" valign="top">
                        <td colspan="9"><table width="816" border="0" cellpadding="0" cellspacing="0">
                            <tr>
                              <td width="1%" height="20"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                              <td width="6%">&nbsp;</td>
                              <td width="1%"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                              <td width="11%">&nbsp;</td>
                              <td width="1%"><img src="imagens/pix_branco.gif" width="10" height="5"></td>
                              <td width="23%">&nbsp;</td>
                              <td width="57%" colspan="10">&nbsp;</td>
                            </tr>
                          </table></td>
                      </tr>
                    </table>
                    <table width="95%" border="0" cellpadding="5" cellspacing="1">
                      <tr>
                        <td class="ativo"><table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#FFFFFF">
			   <span class="texto"><center><?php echo $mensagem;?></center></span>

                            <tr align="center" bgcolor="#38708F" class="texttab">
                              <td align="left" valign="top" bgcolor="9dd1d7">
                                <span class="ativo">Constru&ccedil;&atilde;o de
                                texto do Grupo <?php echo $grupo;?></span></td>
                            </tr>
			   <form action="insereWikiConteudo.php" method="post">
                            <tr align="center" bgcolor="#38708F" class="texttab">
                              <td valign="top" bgcolor="d3f2f5"> <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#FFFFFF">
                                  <tr align="center" valign="middle" class="texto">
                                    <td colspan="3" align="left" bgcolor="d3f2f5">
                                      <p align="left" class="texto">
					<?php 
						if( ($_SESSION['idTipoUsuario'] == "2") && ( ($grupo == $objetoGrupo->grupo) || ($grupo == "0") ) ){						
							if($objeto->topico){
					?>			T&iacute;tulo								
                                        			<input name="titulo" type="text" value="<?php echo $objeto->topico;?>" size="58" <?php echo $disabled;?>>
					<?php 		}else{ ?>
								T&iacute;tulo
								<input name="titulo" type="text" size="58" <?php echo $disabled;?>>
					<?php 		}

						}else{?>
							<span class="texto">
								<?php 

									$enter	 = array("\r\n", "\n", "\r");
									$br	 = '<br />';
									if($objeto->topico){
										$topico   = str_replace($enter, $br, $objeto->topico);
 										echo $objeto->topico;
									}else{
										echo "-";
									}
							}

								?>
							</span>
                                      </p>
				    </td>
                                  </tr>
				  <input type="hidden" name="idTopico" value="<?php echo $idTopico;?>">
                                  <tr align="center" valign="middle" class="texto">
                                    <td colspan="3" align="left" valign="top" bgcolor="F5FEFF">
				      <?php if( ($_SESSION['idTipoUsuario'] == "2") && ( ($grupo == $objetoGrupo->grupo) || ($grupo == "0") )){?> 
						Texto	   	
                                      		<textarea name="conteudo" id="txtarea" cols="60" rows="25" <?php echo $disabled;?> onfocus="mudaCor(this.id)"><?php echo $objeto->conteudo;?>
						</textarea>

				      <?php }else{
						if($objeto->conteudo){
							echo $objeto->conteudo;
				      		}else{
							echo "-";
						}
					     }
				       ?>
				    </td>
                                  </tr>
				  <?php if(($_SESSION['idTipoUsuario'] == "2") && ( ($grupo == $objetoGrupo->grupo) || ($grupo == "0") )){?>
                                  <tr align="center" valign="middle" class="texto">
                                    <td colspan="3" align="left" bgcolor="F5FEFF" class="texto2">
                                      <input type="submit" name="Submit2" value="Enviar o texto modificado"></td>
                                  </tr>
				  <?php } ?>
				  </form>
                                  <tr align="center" valign="middle" class="texto">
                                    <td width="61%" align="left" bgcolor="b4dee3" class="texto2"><strong>Participa&ccedil;&otilde;es
                                      na constru&ccedil;&atilde;o deste texto</strong></td>
                                    <td width="12%" align="center" bgcolor="b4dee3" class="texto2">&nbsp;</td>
                                    <td width="27%" align="right" bgcolor="b4dee3" class="texto2"><strong>Em</strong></td>
                                  </tr>
			          <?php
					$queryUsuarios     = "SELECT usuarios.nome,wikiConteudoUsuarios.dataHora FROM usuarios,wikiConteudoUsuarios WHERE wikiConteudoUsuarios.idConteudo='".$objeto->idConteudo."' AND usuarios.idUsuario=wikiConteudoUsuarios.idUsuario";
					$resultadoUsuarios = mysql_query($queryUsuarios,$conexao);
					while($objetoUsuarios = mysql_fetch_object($resultadoUsuarios)){

			          ?>

                                  <tr align="center" valign="middle" class="texto">
                                    <td align="left" bgcolor="d3f2f5" class="texto2"><strong><?php echo $objetoUsuarios->nome;?></strong></td>
                                    <td align="center" bgcolor="d3f2f5" class="texto2"><table width="40%" border="0" cellspacing="1" cellpadding="5">
                                        <tr>
                                          <td bgcolor="">&nbsp;</td>
                                        </tr>
                                      </table></td>
                                    <td align="right" bgcolor="d3f2f5" class="miudo"><?php echo $objetoUsuarios->dataHora;?></td>
                                  </tr>
				  <?php } ?>
                                </table></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table>
                    <p>&nbsp;</p></td>
                  <td width="3%" align="right" background="imagens/bar_fnd_2.jpg"><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="conteudo.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_2.jpg" width="23" height="113" border="0"></a></strong></td>
                  <td width="8%" align="right" background="imagens/bar_fnd_1.jpg" bgcolor="F1FEF9">
                    <p><strong><a <?php if(!$disabled){?> onclick="deslogaUsuario(1)" <?php } ?> href="principal.php?idTopico=<?php echo $idTopico;?>"><img src="imagens/mn_1.jpg" width="23" height="103" border="0"></a></strong></p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p></td>
                </tr>
              </table></td>
          </tr>
        </table></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</div>
</body>
</html>
